package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import main.AppConstants;

/**
 * The DiscIcon class extends ImageIcon. It's used to present a disc for the
 * game with a specific color. The color used is the one provided within the
 * constructor or called by DiscIcon.setColor.
 * 
 * The class also provides the ability to scale the DiscIcon by providing a
 * second parameter within the constructor (double scale) or by explicity call
 * DiscColor.setScale(double scale) method.
 */
public class DiscIcon extends ImageIcon {

	private Image image = new ImageIcon(getClass().getClassLoader().getResource(AppConstants.IMAGE_DISC)).getImage();
	private Color color;
	private double scale = 1;

	public DiscIcon(Color color) {
		this.color = color;
		setImage(image);
	}

	public DiscIcon() {
		this(Color.RED);
	}
	
	/** Optional constructor with scale parameter, to scale the ImageIcon size */
	public DiscIcon(Color color, double scale) {
		this.color = new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
		this.scale = scale;
		setImage(image);
	}
	
	/** Sets DiscIcon scale */
	public void setScale(double scale) {
		this.scale = scale;
	}
	
	/** Returns the scale */
	public double getScale() {
		return scale;
	}

	/** Set color for the disc object */
	public void setColor(Color color) {
		this.color = color;
	}

	/** Return the disc object's color */
	public Color getColor() {
		return color;
	}

	@Override
	public int getIconHeight() {
		return (int) ((double) image.getHeight(null) * scale);
	}

	@Override
	public int getIconWidth() {
		return (int) ((double) image.getWidth(null) * scale);
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2D = (Graphics2D) g;		// use G2D so we can use scale() and setRenderingHints()
		g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2D.scale(scale, scale);				// Scale the image
		g2D.drawImage(getImage(), x, y, c);	// Drawin image so that setXORmode() doesn't invert the image color on dark bg:
		g2D.setXORMode(color);				// Blend the color:
		g2D.drawImage(getImage(), x, y, c);	// Draw image again
	}
}